package base_05_reflect;

import java.lang.reflect.InvocationTargetException;
import java.util.Random;

/**
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/3/24 16:36
 */
final public class ClassDemo {
    private Integer age;
    private String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //获取Class的三种方式
        Class<ClassDemo> clazz1 = ClassDemo.class;
        ClassDemo demo = new ClassDemo();
        Class<? extends ClassDemo> clazz2 = demo.getClass();
        Class<?> clazz3 = Class.forName("base_05_reflect.ClassDemo");

        System.out.println(clazz1);
        System.out.println(clazz2);
        System.out.println(clazz3);

        //获取类名
        System.out.println(clazz1.getName());
        System.out.println(clazz1.getSimpleName());
        System.out.println(clazz1.getCanonicalName());
        Class<testEnum> testEnumClass = testEnum.class;
        System.out.println(testEnumClass.getName());
        System.out.println(testEnumClass.getCanonicalName());
        System.out.println("=================反射获取类实例======================");
        try {
            ClassDemo demo1 = clazz1.newInstance();
            System.out.println(demo1);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
    }

    private void work() {

    }

    private int work1() {
        return new Random().nextInt();
    }

    private void work2(String str) {
        System.out.println(str);
    }

    private Integer work4(String str) {
        return str.length();
    }

    enum testEnum {
        APPLE, BANANA,

    }

}
